package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.BizEnum;
import com.qk.common.exception.BizException;
import com.qk.dto.activity.AddActivityDTO;
import com.qk.dto.activity.QueryPageActivityDTO;
import com.qk.dto.activity.UpdateActivityDTO;
import com.qk.entity.Activity;
import com.qk.entity.Clue;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author:gzc
 * @Date: 2025/10/06 21:09
 * @Description:
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ClueMapper clueMapper;
    @Override
    public PageResult<Activity> queryListByPage(QueryPageActivityDTO queryPageActivityDTO) {
//        PageInfo<Activity> pageInfo = PageHelper.startPage(activityQueryListByPageDto.getPage(), activityQueryListByPageDto.getPageSize())
//                .doSelectPageInfo(() -> activityMapper.queryPage(activityQueryListByPageDto));
//        return PageResult.<Activity>builder().total(pageInfo.getTotal()).rows(pageInfo.getList()).build();
//        Page<Activity> page = new Page<>(queryPageActivityDTO.getPage(), queryPageActivityDTO.getPageSize());
//        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(queryPageActivityDTO.getChannel() != null, Activity::getChannel, queryPageActivityDTO.getChannel())
//                .eq(queryPageActivityDTO.getType() != null, Activity::getType, queryPageActivityDTO.getType())
//                .gt(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 1, Activity::getStartTime, LocalDateTime.now())
//                .le(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 2, Activity::getStartTime, LocalDateTime.now())
//                .ge(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 2, Activity::getEndTime, LocalDateTime.now())
//                .lt(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 3, Activity::getEndTime, LocalDateTime.now())
//                .orderByDesc(Activity::getUpdateTime);
//        IPage<Activity> iPage = activityMapper.selectPage(page, queryWrapper);
        Page<Activity> page = this.page(new Page<>(queryPageActivityDTO.getPage(), queryPageActivityDTO.getPageSize()), Wrappers.lambdaQuery(Activity.class)
                .eq(queryPageActivityDTO.getChannel() != null, Activity::getChannel, queryPageActivityDTO.getChannel())
                .eq(queryPageActivityDTO.getType() != null, Activity::getType, queryPageActivityDTO.getType())
                .gt(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 1, Activity::getStartTime, LocalDateTime.now())
                .le(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 2, Activity::getStartTime, LocalDateTime.now())
                .ge(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 2, Activity::getEndTime, LocalDateTime.now())
                .lt(queryPageActivityDTO.getStatus() != null && queryPageActivityDTO.getStatus() == 3, Activity::getEndTime, LocalDateTime.now())
                .orderByDesc(Activity::getUpdateTime));
        return PageResult.<Activity>builder().total(page.getTotal()).rows(page.getRecords()).build();
    }

    @Override
    public void deleteById(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        List<Clue> clues = clueMapper.selectList(Wrappers.lambdaQuery(Clue.class).eq(Clue::getActivityId, id));
        if (!ObjectUtil.isEmpty(clues)){
            BizException.throwBizException(BizEnum.ACTIVITY_CLUE_EXIST);
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void addActivity(AddActivityDTO addActivityDTO) {
        boolean hasNull = BeanUtil.hasNullField(addActivityDTO, "discount", "voucher");
        if (hasNull) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Activity activity = BeanUtil.copyProperties(addActivityDTO, Activity.class);
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        this.save(activity);
    }

    @Override
    public Activity queryById(Integer id) {
        if (Objects.isNull(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        return this.getById(id);
    }

    @Override
    public void updateActivity(UpdateActivityDTO updateActivityDTO) {
        boolean hasNull = BeanUtil.hasNullField(updateActivityDTO, "discount", "voucher", "createTime", "updateTime");
        if (hasNull) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Activity activity = BeanUtil.copyProperties(updateActivityDTO, Activity.class);
        activity.setUpdateTime(LocalDateTime.now());
        this.updateById(activity);
    }

    @Override
    public List<Activity> queryByType(Integer type) {
        if (Objects.isNull(type)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getType, type);
        List<Activity> activitiesList = this.baseMapper.selectList(queryWrapper);
        return activitiesList;
    }

}
